<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
require_once("action/conn.php");
require_once("action/common.php");

$tablename = getParameter("tablename");
$table = "t_a_game_" . $tablename;

$oldcode = checkTrim($_POST['oldcode']);
$code = checkTrim($_POST['code']);
$name = checkTrim($_POST['name']);
$isModify = $_POST['isModify'];
$answer1 = $_POST['answer11'] . "-" . (checkTrim($_POST['answer12']) == "" ? $_POST['answer11'] : $_POST['answer12']);
$answer2 = $_POST['answer21'] . "-" . (checkTrim($_POST['answer22']) == "" ? $_POST['answer21'] : $_POST['answer22']);
$answer3 = $_POST['answer31'] . "-" . (checkTrim($_POST['answer32']) == "" ? $_POST['answer31'] : $_POST['answer32']);
$answer4 = $_POST['answer41'] . "-" . (checkTrim($_POST['answer42']) == "" ? $_POST['answer41'] : $_POST['answer42']);
$voice1 = $_POST['voice1'];
$voice2 = $_POST['voice2'];
$pic = $_POST['pic'];
$difficult = $_POST['difficult'];

$db = new DBC();
$is_code_exist = false;
if ($isModify == 1) {
    $sql = "UPDATE " . $table . " SET code = '" . $code . "',name = '" . $name . "',answer1 = '" . $answer1 . "',answer2 = '" . $answer2 . "',answer3 = '" . $answer3 . "',answer4 = '" . $answer4
            . "',voice1='" . $voice1 . "',status = 0,voice2 = '" . $voice2 . "',pic = '" . $pic . "',difficult = '" . $difficult . "',updateTime=now() WHERE code = '" . $oldcode . "'";
} else {
    $sql = "SELECT code FROM " . $table . " WHERE code= '" . $code . "'";
    $data = $db->fetchData($sql);
    if ($data != null && count($data) > 0) {
        $is_code_exist = true;
    }
    $sql = "INSERT INTO " . $table . "(code,name,answer1,answer2,answer3,answer4,voice1,voice2,pic,difficult,updateTime,createTime) VALUES('"
            . $code . "','" . $name . "','" . $answer1 . "','" . $answer2 . "','" . $answer3 . "','" . $answer4 . "','" . $voice1 . "','" . $voice2 . "','" . $pic . "','" . $difficult . "',now(),now())";
}

if ($is_code_exist) {
    $msg = "编号 " . $code . " 已经存在。";
    echo "<script language='javascript'>alert('" . $msg . "'); </script>";
    echo "<script language='javascript'>history.back();</script>";
} else {
    if (isset($_POST['page'])) {
        $page = $_POST['page'];
    } else {
        $page = 0;
    }
    $transfer = "questionTitlePacketList.php?menu=questionTitlePacket&page=" . $page . "&tablename=" . $name;
    if ($db->execute($sql)) {
        $row = array();

        $row['code'] = $code;
        $row['name'] = $name;
        $row['voice1'] = $voice1;
        $row['voice2'] = $voice2;
        $row['pic'] = $pic;
        $row['answer1'] = $answer1;
        $row['answer2'] = $answer2;
        $row['answer3'] = $answer3;
        $row['answer4'] = $answer4;
        $row['difficult'] = $difficult;

        createXML($row);
        echo $transfer;
        //success
        //    echo "<script language='javascript'>alert('成功'); </ script>";
        echo "<script language='javascript'>location='" . $transfer . "';</script>";
    } else {
        //failue
        echo "<script language='javascript'>alert('失败'); </script>";
        echo "<script language='javascript'>history.back();</script>";
    }
}

function buildXML($row) {
    $doc = new DOMDocument('1.0', 'UTF-8');
    $doc->formatOutput = TRUE;
    $r = $doc->createElement("question");
    $doc->appendChild($r);
    $code = $doc->createElement("code");
    $code->appendChild($doc->createTextNode($row['code']));
    $r->appendChild($code);
    $name = $doc->createElement("name");
    $name->appendChild($doc->createTextNode($row['name']));
    $r->appendChild($name);
    $answer1 = $doc->createElement("answer1");
    $answer1->appendChild($doc->createTextNode($row['answer1']));
    $r->appendChild($answer1);
    $answer2 = $doc->createElement("answer2");
    $answer2->appendChild($doc->createTextNode($row['answer2']));
    $r->appendChild($answer2);
    $answer3 = $doc->createElement("answer3");
    $answer3->appendChild($doc->createTextNode($row['answer3']));
    $r->appendChild($answer3);
    $answer4 = $doc->createElement("answer4");
    $answer4->appendChild($doc->createTextNode($row['answer4']));
    $r->appendChild($answer4);
    $voice1 = $doc->createElement("voice1");
    $voice1->appendChild($doc->createTextNode($row['voice1']));
    $r->appendChild($voice1);
    $voice2 = $doc->createElement("voice2");
    $voice2->appendChild($doc->createTextNode($row['voice2']));
    $r->appendChild($voice2);
    $pic = $doc->createElement("pic");
    $pic->appendChild($doc->createTextNode($row['pic']));
    $r->appendChild($pic);
    $difficult = $doc->createElement("difficult");
    $difficult->appendChild($doc->createTextNode($row['difficult']));
    $r->appendChild($difficult);
    //echo $doc->saveXML();
    //$file = "../game/xml/" . $row['code'] . ".xml";
    $file = "../game/xml/" . $row['code'] . $row['difficult'] . ".xml";
    $doc->save($file);
}

function createXML($row) {
    $xml = '<?xml version="1.0" encoding="utf-8"?>';
    $xml = $xml . '<UserData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">';
    $xml = $xml . '<question>';
    $xml = $xml . '<code>' . $row['code'] . '</code>';
    $xml = $xml . '<name>' . $row['name'] . '</name>';
    $xml = $xml . '<answer1>' . $row['answer1'] . '</answer1>';
    $xml = $xml . '<answer2>' . $row['answer2'] . '</answer2>';
    $xml = $xml . '<answer3>' . $row['answer3'] . '</answer3>';
    $xml = $xml . '<answer4>' . $row['answer4'] . '</answer4>';
    $xml = $xml . '<voice1>' . $row['voice1'] . '</voice1>';
    $xml = $xml . '<voice2>' . $row['voice2'] . '</voice2>';
    $xml = $xml . '<pic>' . $row['pic'] . '</pic>';
    $xml = $xml . '<difficult>' . $row['difficult'] . '</difficult>';
    $xml = $xml . '</question>';
    $xml = $xml . '</UserData>';
    $filename = "../game/xml/" . $row['code'] . $row['difficult'] . ".xml";
    $file = fopen($filename, "w");
    fwrite($file, $xml);
    fclose($file);
}

function checkTrim($item) {
    if (isset($item)) {
        $item = trim($item);
    } else {
        $item = "";
    }
    return $item;
}
?>
